<?php
class main extends spController {

	function index() {

		$openid = $this -> spArgs('openid');
		$openkey = $this -> spArgs('openkey');

		if (!empty($openid)) {
			$this -> loginapp();
		} else {
			//模拟登录
			// $args = array('openid' => '000', 'openkey' => 'B811C64D348DA42250B37BE081EAF328', 'pf' => 'qzone');
			// $url = spUrl('main', 'loginapp', $args);
			// echo "<a href='{$url}'>{$url}</a>";
			$this -> display("notice.html");
		}

	}

	public function loginapp() {

		$openid = $this -> spArgs('openid');
		$openkey = $this -> spArgs('openkey');
		$pf = $this -> spArgs('pf');
		$app_custom = $this -> spArgs('app_custom');
		$xz = 10;
		//初始相纸数量
		//取QQ用户信息
		$qqinfo = spClass('QQinfo');
		$args = array('openid' => $openid, 'openkey' => $openkey);
		$rs = $qqinfo -> getUserInfo($args);
		if ($rs['ret'] == 0) {
			$now = date("Y-m-d H:i:s", time());
			$new_user = array();
			$condition = array("openid" => $openid);
			if ($new_user = spDB('ss_user') -> find($condition)) {
				//更新信息
				$row = array('mdfdate' => $now, 'lastlogin' => $now, 'nickname' => $rs['nickname'], 'gender' => $rs['gender'], 'country' => $rs['country'], 'province' => $rs['province'], 'city' => $rs['city'], 'figureurl' => str_replace("/50", "/30", $rs['figureurl']));
				$condition = array('id' => $new_user['id']);
				spDB('ss_user') -> update($condition, $row);
			} else {
				//新用户注册
				$new_counts = array('times' => 0, 'xz' => $xz, 'createdate' => $now, 'mdfdate' => $now, 'status' => 1);
				$new_user = array('openid' => $openid, 'nickname' => $rs['nickname'], 'gender' => $rs['gender'], 'country' => $rs['country'], 'province' => $rs['province'], 'city' => $rs['city'], 'figureurl' => str_replace("/50", "/30", $rs['figureurl']), 'appcustom' => $app_custom, 'createdate' => $now, 'mdfdate' => $now, 'status' => '1', 'counts' => $new_counts);
				$new_user['id'] = spClass('user') -> spLinker() -> create($new_user);
				//print_r($new_user);
			}
			$this -> user = $new_user;
			$this -> openid = $openid;
			$this -> openkey = $openkey;

			//IMG_SERVER 配置见 include/config.php
			$this -> pf = $pf;
			$this -> userid = $new_user['id'];
			$this -> vars = "http://" . IMG_SERVER . "/snapshot/snapshot.swf?userid={$new_user['id']}&nickname={$new_user['nickname']}&figureurl={$new_user['figureurl']}&gender={$new_user['gender']}&country={$new_user['country']}&province={$new_user['province']}&city={$new_user['city']}&appserver=" . APP_SERVER . "&imgserver=" . IMG_SERVER;
			$this -> display('main.html');

		} else {
			$this -> display('error.html');
		}
	}

	public function savepic() {
		if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {
			$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
			import("FileUtils.php");
			$today = date("Ymd");
			$now = date("Y-m-d H:i:s", time());
			$dir = "/snapshot/upload/{$today}" . "/";
			$fileno = FileUtils::getSN();
			$url = $dir . $fileno . ".jpg";

			import('UpYun.php');
			$upyun = new UpYun('doucome-app-static', 'snapshot', '3edc4rfv');

			$rsp = $upyun -> writeFile($url, $jpg, True);

			if (!empty($rsp)) {
				$userid = $this -> spArgs('userid');
				$gender = $this -> spArgs('gender');
				//图片信息
				$new_photo = array('userid' => $userid, 'url' => $url, 'createdate' => $now, 'mdfdate' => $now, 'no' => $fileno, 'gender' => $gender,'status'=>'1');
				$photo = spDB('ss_photo');
				$photoid = $photo -> create($new_photo);
				echo "ok";
			} else {
				echo "upload failed!";
			}

		} else {

			echo "Encoded JPEG information not received.";
		}

	}

	public function show() {

		$userid = $this -> spArgs('userid');
		$gender = $this -> spArgs('gender');
		if (!empty($userid)) {

			//匹配TA
			//从图片里面取
			$sql = "select t1.id as photoid,t1.userid,t1.url,t3.nickname,t3.province,t3.city,t3.figureurl
					from ss_photo as t1,ss_user t3
					where t1.gender<>'$gender'
					and t1.userid<>$userid
					and t1.id not in(select t2.photoid from ss_fen as t2 where t2.pfid=$userid and t2.photoid=t1.id)
					and t1.userid=t3.id
					and t1.status='1'
					order by t1.createdate 
					limit 20 ";
			//echo $sql;
			$query = spDB('ss_photo');
			$rs = $query -> findSql($sql);
			if (empty($rs)) {$rs = array('photoid' => 0, 'userid' => 0, 'url' => '/snapshot/sample.jpg');
				echo json_encode(array($rs));
			} else {
				$ct = count($rs);
				$i = rand(0, $ct - 1);
				echo json_encode(array($rs[$i]));
			}

			//echo json_encode($rs);

		} else {
			echo json_encode(array( array('photoid' => 0, 'userid' => 0, 'url' => '/snapshot/sample.jpg')));
		}

	}

	public function test() {

		// $rs=spDB('ss_user')->findAll();
		// dump($rs);
		// echo "good luck";
		//$this -> userid = 0;

		$this -> display('photowall.html');
	}

	public function ajax() {

		echo "good luck";
	}

	public function pingfen($value = '') {
		$fen = $_GET['fen'];
		$photoid = $_GET['photoid'];
		$photo_userid = $_GET['photo_userid'];
		$pfid = $_GET['pfid'];
		$now = date("Y-m-d H:i:s", time());
		$new_fen = array("userid" => $photo_userid, "fen" => $fen, 'createdate' => $now, 'mdfdate' => $now, "status" => '', 'pfid' => $pfid, 'photoid' => $photoid);
		spDB('ss_fen') -> create($new_fen);
		echo "ping fen ok!";
	}

	public function jubao() {
		$submitid = $_GET['submitid'];
		$jubaoid = $_GET['jubaoid'];
		$photoid = $_GET['photoid'];
		$type = '1';
		$now = date("Y-m-d H:i:s", time());
		$new_jubao = array("submitid" => $submitid, "jubaoid" => $jubaoid, "photoid" => $photoid, "type" => 1, 'createdate' => $now, 'mdfdate' => $now, "status" => '');
		spDB('ss_jubao') -> create($new_jubao);
		echo "jubao ok!";
	}

	public function like() {
		$submitid = $this -> spArgs('submitid');
		$likeid = $this -> spArgs('likeid');
		$photoid = $this -> spArgs('photoid');
		$type = '1';
		$now = date("Y-m-d H:i:s", time());
		$new_like = array("submitid" => $submitid, "likeid" => $likeid, "photoid" => $photoid, "type" => 1, 'createdate' => $now, 'mdfdate' => $now, "status" => '');
		spDB('ss_likes') -> create($new_like);
		echo "ok";
	}

	public function sync() {
		$userid = $this -> spArgs('userid');
		$user = spClass("user");
		$rs = $user -> spLinker() -> findBy('id', $userid);
		$sync = array( array('xz' => $rs['counts']['xz'], 'times' => $rs['counts']['times'], 'fen' => $rs['fen']['fen'], 'likes' => $rs['likes']));
		echo json_encode($sync);
	}

	public function upcount() {
		$userid = $this -> spArgs('userid');
		$type = $this -> spArgs('type');
		$value = $this -> spArgs('value');
		$counts = spClass("counts");
		$conditions = array('userid' => $userid);
		switch ($type) {
			case 1 :
				$counts -> incrField(array('userid' => $userid), 'times', $value);
				break;
			case 2 :
				$counts -> decrField(array('userid' => $userid), 'xz', $value);
				break;
			default :
				break;
		}
		$rs = $counts -> update();
		echo "upcount ok!";
	}

	public function upxz() {
		$userid = $this -> spArgs('userid');
		$type = $this -> spArgs('type');
		$now = date("Y-m-d H:i:s", time());
		$max = 5;
		//判断是否到达当日分享上限
		$share = spClass("share");
		$conditions = array('userid' => $userid, 'type' => $type, 'date(createdate)' => date("Y-m-d", time()));
		$ttl = $share -> findCount($conditions);
		if ($ttl < $max) {
			$counts = spClass("counts");
			$counts -> incrField(array('userid' => $userid), 'xz');
			$new_share = array('userid' => $userid, 'type' => $type, 'createdate' => $now, 'mdfdate' => $now, 'status' => 1);
			$share -> create($new_share);
			echo "获得一张相纸！";
		} else {
			echo "抱歉，已经到达当日获赠上限($max 张)，不能再获得赠送！";
		}

	}

	

}
